package java8.deom5;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;




import org.junit.Test;

import java8.bean.Artist;

public class Tests1 {

	List<Artist> allArtists = new ArrayList<>();

	private void init() {
		allArtists.add(new Artist("Artist_a", "a", "guangzhou"));
		allArtists.add(new Artist("Artist_b", "b", "foshan"));
		allArtists.add(new Artist("Artist_c", "c", "shenzhen"));

	}

	@Test
	public void test1() {
		init();
		long count = allArtists.parallelStream().filter(artist -> artist.getOrigin().equals("foshan")).count();
		System.out.println(count);
	}

	@Test
	public void test2() {
		List<Integer> list = Stream.of(1, 2, 3, 4).collect(Collectors.toList());
		System.out.println(addIntegers(list));
	}

	/**
	 * 并行求和
	 * 
	 * @param values
	 * @return
	 */
	private int addIntegers(List<Integer> values) {
		return values.parallelStream().mapToInt(i -> i).sum();
	}

	/**
	 * 传统初始化数字
	 * @param size
	 * @return
	 */
	public static double[] imperativeInitilize(int size) {
		double[] values = new double[size];
		for (int i = 0; i < values.length; i++) {
			values[i] = i;
		}
		return values;
	}

	/**
	 * 使用 并行化数组操作初始化数组
	 * @param size
	 * @return
	 */
	public static double[ ] parallelInitialize(int size) {
		double[ ] values = new double[ size] ;
		Arrays.parallelSetAll(values, i -> i) ;
		return values;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
